Imports capaLogica
Public Class VentanaVerAusencias
    Private idEstudiante As String
    Private gestor As GestorEquipo
    Private gestorAsistencia As GestorAsistencia
    Dim estaCargado As Boolean
    Dim indexFilaModificada As Integer
    Dim id As Integer

    Public Sub New(ByVal idEstudiante As String)
        Me.InitializeComponent()
        Me.idEstudiante = idEstudiante
        Me.gestor = New GestorEquipo()
        Me.gestorAsistencia = New GestorAsistencia()
        estaCargado = False
    End Sub

    Private Sub VentanaVerAusencias_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CargarAusencias()
    End Sub

    Private Sub CargarAusencias()
        Dim ausencias As DataTable = gestorAsistencia.LevantarAusencias(idEstudiante)

        DataGridViewVerAusencias.DataSource = ausencias
        With Me.DataGridViewVerAusencias.Columns.Item("idAusencia")
            .Visible = False
        End With
        With Me.DataGridViewVerAusencias.Columns.Item("fecha")
            .HeaderText = "Fecha"
            .DisplayIndex = 2
            .Name = "fecha"
            .ReadOnly = True
        End With
        With Me.DataGridViewVerAusencias.Columns.Item("estaJustificada")
            .HeaderText = "Justificada"
            .DisplayIndex = 3
            .Name = "justificada"
        End With
        With Me.DataGridViewVerAusencias.Columns.Item("motivo")
            .HeaderText = "Justificacion"
            .DisplayIndex = 4
            .Width = 316

        End With
        With Me.DataGridViewVerAusencias.Columns.Item("idEstudiante")
            .HeaderText = "Carnet"
            .DisplayIndex = 1
        End With
        With Me.DataGridViewVerAusencias.Columns.Item("idGrupo")
            .Visible = False
        End With
        estaCargado = True
    End Sub

    Private Sub DataGridViewVerAusencias_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewVerAusencias.CellEnter
        If estaCargado Then
            indexFilaModificada = Me.DataGridViewVerAusencias.CurrentRow.Index
            If Me.DataGridViewVerAusencias.Rows(indexFilaModificada).Cells(0).Value IsNot System.DBNull.Value Then

                id = Me.DataGridViewVerAusencias.Rows(indexFilaModificada).Cells(0).Value
            End If

        End If
    End Sub

    Private Sub DataGridViewVerAusencias_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewVerAusencias.CellEndEdit
        If estaCargado Then
            Dim estaJustificada As Boolean
            Dim motivo As String

            If Me.DataGridViewVerAusencias.Rows(indexFilaModificada).Cells(2).Value IsNot System.DBNull.Value Then
                estaJustificada = Me.DataGridViewVerAusencias.Rows(indexFilaModificada).Cells(2).Value
            Else
                estaJustificada = False
            End If

            If Me.DataGridViewVerAusencias.Rows(indexFilaModificada).Cells(3).Value IsNot System.DBNull.Value Then
                motivo = Me.DataGridViewVerAusencias.Rows(indexFilaModificada).Cells(3).Value
            Else
                motivo = ""
            End If

            gestorAsistencia.ModificarAusencia(estaJustificada, motivo, id)
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Me.Close()
    End Sub
End Class